<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 登录logo -->
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <!-- 登录表单 -->
            <el-form ref="loginFormRef" :model="loginForm" :rules="rules" label-width="0" class="login_form">
                <el-form-item  prop="username">
                    <!-- 引用的阿里图标库 -->
                    <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
                 </el-form-item>
                 <!-- 密码 -->
                <el-form-item  prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>

        </div>
    </div>
</template>

<script>
export default {
    data: function() {
        return {
            loginForm: {
                 username: 'admin',
                 password: '123456'
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        resetLoginForm() {
            // $refs是表单ref的引用，然后通过名字掉重置方法，实现 重置按钮的功能
            this.$refs.loginFormRef.resetFields()
        },
        login() {
            this.$refs.loginFormRef.validate(async valid => {
                // validate先进行预校验，对输入的内容进行验证，得到一个boolean
                // 验证失败，直接return
                if (!valid) return
                // 利用对象解构，把拿到的对象中的data值，直接赋值给res
                const { data: res } = await this.$http.post('login', this.loginForm)
                console.log(res)
                // 状态码不为200，消息提示登录失败
                if (res.meta.status !== 200) return this.$message.error('登录失败，' + res.meta.msg)
                this.$message.success('登录成功')
                // 1.登录成功后，将token保存在客户端
                const tokenStr = res.data.token
                window.sessionStorage.setItem('token', tokenStr)
                // 2.通过编程式导航跳转到home主页
                this.$router.push('/home')
            })
        }
    }
}
</script>

<style lang="less" scoped>
.login_container {
    background-color: #2b4b6b;
    height: 100%;
}
.login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    border-radius: 5px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}
.avatar_box {

    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    position: absolute;
    border-radius: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 10px;
    background-color: #fff;
    box-shadow: 0 0 10px #ddd;
    img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
    }
}
.btns {
    display: flex;
    justify-content: flex-end;
}
.login_form {
    width: 100%;
    position: absolute;
    bottom: 0;
    padding: 0 20px;
    box-sizing: border-box;
}
</style>
